package com.huayi.sukai;

import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import io.dcloud.common.util.JSUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * Created by mac on 2017/3/30.
 */

public class Sukai extends StandardFeature {

    private String TAG = Sukai.class.getName();
    public static String OPEN_LINK = "open_sukai_link";
    private Context context;

    public void onStart(Context pContext, Bundle pSavedInstanceState, String[] pRuntimeArgs) {
        Log.e(TAG,"plugin onstart...");
        /**
         * 如果需要在应用启动时进行初始化，可以继承这个方法，并在properties.xml文件的service节点添加扩展插件的注册即可触发onStart方法
         * */
        this.context = pContext;
    }

    public void OpenLink(IWebview pWebview, JSONArray array){
        String url = array.optString(0);
        //
        Intent intent = new Intent();
        intent.setAction(OPEN_LINK);
        intent.putExtra("url",url);
        this.context.sendBroadcast(intent);
    }

    public void PluginTestFunction(IWebview pWebview, JSONArray array)
    {
        // 原生代码中获取JS层传递的参数，
        // 参数的获取顺序与JS层传递的顺序一致
        String CallBackID = array.optString(0);
        JSONArray newArray = new JSONArray();
        newArray.put(array.optString(1));
        newArray.put(array.optString(2));
        newArray.put(array.optString(3));
        newArray.put(array.optString(4));
        // 调用方法将原生代码的执行结果返回给js层并触发相应的JS层回调函数
        JSUtil.execCallback(pWebview, CallBackID, newArray, JSUtil.OK, false);

    }
}
